package com.cqh.recruit.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqh.recruit.mapper.EmployeeMapper;
import com.cqh.recruit.pojo.dto.EmployeeLoginDTO;
import com.cqh.recruit.pojo.dto.MEmployeeQueryDto;
import com.cqh.recruit.pojo.entity.Employee;
import com.cqh.recruit.pojo.entity.LoginInfo;
import com.cqh.recruit.pojo.vo.MEmployeeListVo;
import com.cqh.recruit.service.IEmployeeService;
import com.cqh.recruit.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工信息表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2025-10-18
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param employeeLoginDto
     * @return
     */
    @Override
    public LoginInfo login(EmployeeLoginDTO employeeLoginDto) {
        //调用Mapper接口，根据账号和密码查询用户
        Employee employee=employeeMapper.login(employeeLoginDto);

        //2.判断账号和密码是否存在
        if (employee != null) {
            //生成JWT 令牌
            Map<String, Object> claims = new HashMap<>();//存放学生信息
            claims.put("empId", employee.getId());//学生id
            claims.put("account", employee.getAccount());//学生账号
            claims.put("name", employee.getName());//学生姓名
            String jwt = JwtUtils.generateToken(claims);//生成JWT令牌

            return new LoginInfo(employee.getId(),employee.getAccount(), employee.getName(), employee.getAvatar(),jwt);
        }
        return null;
    }

    @Override
    public Page<MEmployeeListVo> findEmployeeByQueryDto(MEmployeeQueryDto mEmployeeQueryDto) {
        Page page=new Page(mEmployeeQueryDto.getPage(),mEmployeeQueryDto.getLimit());
        return getBaseMapper().findEmployeeByQueryDto(page,mEmployeeQueryDto);
    }
}
